package com.yksj.healthtalk.adapter;

import java.util.List;

import org.universalimageloader.core.ImageLoader;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.BuythingEntity;
import com.yksj.healthtalk.ui.R;

@SuppressLint("ResourceAsColor")
public class BuyThingsAdapter extends BaseAdapter {

	private Context mContext;
	private List<BuythingEntity> mBuyThingEntitys=null;
	private LayoutInflater inflater;
	private final ImageLoader mImageLoader;
	private String type;
	onClickHeadSellerNumListener listener;//点击购买者监听
	onClickHeaderListener mHeaderListener;//点击头像监听
	public BuyThingsAdapter(Context context,List<BuythingEntity> mBuyThingEntity,String type) {
		this.mContext=context;
		this.mBuyThingEntitys=mBuyThingEntity;
		this.inflater=LayoutInflater.from(mContext);
		this.type=type;
		this.mImageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		return mBuyThingEntitys.size();
	}

	@Override
	public Object getItem(int position) {
		return mBuyThingEntitys.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		final BuythingEntity entity = mBuyThingEntitys.get(position);
		if(convertView==null){
			holder=new ViewHolder();
			convertView=inflater.inflate(R.layout.buy_thing_adapter,null);
			holder.imge=(ImageView)convertView.findViewById(R.id.imge);
			holder.sellerName=(TextView)convertView.findViewById(R.id.sellerName);
			holder.descript=(TextView)convertView.findViewById(R.id.descript);
			holder.time=(TextView)convertView.findViewById(R.id.time);
			holder.seller=(Button)convertView.findViewById(R.id.right_bottom_button);
			holder.money=(TextView)convertView.findViewById(R.id.money);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder) convertView.getTag();
			}
			holder.sellerName.setText(entity.getSellerName());
			
			if("7".equals(entity.getDescribe())){
				holder.descript.setText("日票"+entity.getSTATUS());
			}else if("8".equals(entity.getDescribe())){
				holder.descript.setText("月票"+entity.getSTATUS());
			}else if("5".equals(entity.getDescribe())){
				holder.descript.setText(entity.getTYPENAME());
			}else{
				holder.descript.setText("..."+entity.getSTATUS());
			}
			
			if(entity.getSTATUS().contains("已退")){
				holder.descript.setText(entity.getTYPENAME()+"("+entity.getSTATUS()+")");
				holder.descript.setTextColor(mContext.getResources().getColor(R.color.tab_txt_orange));
			}else{
				holder.descript.setTextColor(mContext.getResources().getColor(R.color.tab_txt_gray));
			}
			
			holder.time.setText(entity.getDate());
			holder.money.setText(entity.getMoneyNum()+"元");
			mImageLoader.displayImage(entity.getCUSTOMER_SEX(), entity.getAddressImage(),holder.imge);
			
			if(type.equals("2")){//显示购买者按钮
				holder.seller.setVisibility(View.VISIBLE);
			}
			holder.seller.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					listener.onSalonHeadSellerNumClick(holder.seller, entity, position);
				}
			});
			holder.imge.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mHeaderListener.onClickHeaderClick(holder.imge, entity, position);
				}
			});
		return convertView;
	}
	
	static class ViewHolder{
		Button seller;
		ImageView imge;
		TextView sellerName;
		TextView descript;
		TextView time;
		TextView money;
	}
	
	public interface onClickHeadSellerNumListener{
		void onSalonHeadSellerNumClick(View v,BuythingEntity entity,int position);
	}
	
	public void setonClickHeadSellerNumListener(onClickHeadSellerNumListener c){
		this.listener=c;
	}
	
	public interface onClickHeaderListener{
		void onClickHeaderClick(View v,BuythingEntity entity,int position);
	}
	
	public void setonClickHeaderListener(onClickHeaderListener h){
		this.mHeaderListener=h;
	}
}

